/**
 * Copyright &copy; 2012-2017 <a href="https://vivo.com/">vivo</a> All rights reserved.
 */
package com.vivo.dingtalk.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.vivo.dingtalk.entity.DingWorkRecord;
import com.vivo.dingtalk.service.DingWorkRecordService;

import java.util.List;

/**
 * 钉钉备忘信息Controller
 * @author jyangye
 * @version 2018-10-31
 */
@Controller
@RequestMapping(value = "${adminPath}/dingtalk/dingWorkRecord")
public class DingWorkRecordController extends BaseController {

	@Autowired
	private DingWorkRecordService dingWorkRecordService;
	
	@ModelAttribute
	public DingWorkRecord get(@RequestParam(required=false) String id) {
		DingWorkRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dingWorkRecordService.get(id);
		}
		if (entity == null){
			entity = new DingWorkRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("dingtalk:dingWorkRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(DingWorkRecord dingWorkRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DingWorkRecord> page = dingWorkRecordService.findPage(new Page<DingWorkRecord>(request, response), dingWorkRecord); 
		model.addAttribute("page", page);
		return "vivo/dingtalk/dingWorkRecordList";
	}

	@RequiresPermissions("dingtalk:dingWorkRecord:view")
	@RequestMapping(value = "form")
	public String form(DingWorkRecord dingWorkRecord, Model model) {
		model.addAttribute("dingWorkRecord", dingWorkRecord);
		return "vivo/dingtalk/dingWorkRecordForm";
	}

	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
	@RequestMapping(value = "save")
	public String save(DingWorkRecord dingWorkRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dingWorkRecord)){
			return form(dingWorkRecord, model);
		}
		dingWorkRecordService.save(dingWorkRecord);
		addMessage(redirectAttributes, "保存钉钉备忘信息成功");
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
	}
	
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(DingWorkRecord dingWorkRecord, RedirectAttributes redirectAttributes) {
		dingWorkRecordService.delete(dingWorkRecord);
		addMessage(redirectAttributes, "删除钉钉备忘信息成功");
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
	}

	/**
	 * 批量删除钉钉备忘信息
	 */
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			dingWorkRecordService.delete(dingWorkRecordService.get(id));
		}
		addMessage(redirectAttributes, "删除钉钉备忘信息成功");
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
	}

	/**
	 * 批量取消钉钉备忘信息
	 */
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
	@RequestMapping(value = "cancelRW")
	public String cancelRW(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			dingWorkRecordService.delete(dingWorkRecordService.get(id));
		}
		addMessage(redirectAttributes, "删除钉钉备忘信息成功");
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(DingWorkRecord dingWorkRecord, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "钉钉备忘信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<DingWorkRecord> page = dingWorkRecordService.findPage(new Page<DingWorkRecord>(request, response, -1), dingWorkRecord);
    		new ExportExcel("钉钉备忘信息", DingWorkRecord.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出钉钉备忘信息记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<DingWorkRecord> list = ei.getDataList(DingWorkRecord.class);
			for (DingWorkRecord dingWorkRecord : list){
				try{
					dingWorkRecordService.save(dingWorkRecord);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条钉钉备忘信息记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条钉钉备忘信息记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入钉钉备忘信息失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
    }

	/**
	 * 下载导入{functionNameSimple}数据模板
	 */
	@RequiresPermissions("dingtalk:dingWorkRecord:edit")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "钉钉备忘信息数据导入模板.xlsx";
    		List<DingWorkRecord> list = Lists.newArrayList();
    		new ExportExcel("钉钉备忘信息数据", DingWorkRecord.class, 2).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/dingtalk/dingWorkRecord/?repage";
    }

}